<!DOCTYPE html>

<html>
<head>
  <title>logger.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>logger.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>SERVER LOGGER</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Handles server logging using Loggly or Logentries. Please make sure to
set the correct parameters on the (log settings)[settings.html].</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Logger</span></span>

    lodash = require <span class="string">"lodash"</span>
    settings = require <span class="string">"./settings.coffee"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Logging providers will be set on <code>init</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    logentries = <span class="literal">null</span>
    loggly = <span class="literal">null</span>
    logger = <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>The <code>currentIP</code> will be set on init, but only if <code>settings.Log.sendIP</code> is true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    currentIP = <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <h2>INIT</h2>

            </div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>Init the Logger. Verify which service is set, and add the necessary transports.
IP address and timestamp will be appended to logs depending on the <a href="settings.html">settings</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    init: =&gt;
        <span class="keyword">if</span> settings.Log.sendIP
            ifaces = require(<span class="string">"os"</span>).networkInterfaces()
            <span class="keyword">for</span> i <span class="keyword">of</span> ifaces
                ifaces[i].forEach (details) -&gt;
                    <span class="keyword">if</span> details.family <span class="keyword">is</span> <span class="string">"IPv4"</span> <span class="keyword">and</span> <span class="keyword">not</span> details.internal
                        currentIP = details.address

        <span class="keyword">if</span> settings.Log.service <span class="keyword">is</span> <span class="string">"loggly"</span>
            logentries = <span class="literal">null</span>
            loggly = require <span class="string">"loggly"</span>
            logger = loggly.createClient {subdomain: settings.Log.Loggly.subdomain, json: <span class="literal">true</span>}
        <span class="keyword">else</span>
            loggly = <span class="literal">null</span>
            logentries = require <span class="string">"node-logentries"</span>
            logger = logentries.logger {token: settings.Log.Logentries.token, timestamp: settings.Log.sendTimestamp}

        <span class="keyword">if</span> currentIP?
            ipInfo = <span class="string">"IP <span class="subst">#{currentIP}</span>"</span>
        <span class="keyword">else</span>
            ipInfo = <span class="string">"No IP set."</span>

        <span class="keyword">if</span> settings.General.debug
            <span class="property">@info</span> <span class="string">"LOGGING STARTED! (DEBUG MODE)"</span>, ipInfo
        <span class="keyword">else</span>
            <span class="property">@info</span> <span class="string">"LOGGING STARTED!"</span>, ipInfo</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <h2>LOG METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Log any object to the default transports as <code>log</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    info: =&gt;
        console.info.apply <span class="keyword">this</span>, arguments <span class="keyword">if</span> settings.General.debug
        msg = <span class="property">@getMessage</span> arguments

        <span class="keyword">if</span> loggly?
            msg.unshift settings.Log.Loggly.token
            logger.log.apply logger, msg
        <span class="keyword">else</span>
            logger.info.apply logger, msg</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Log any object to the default transports as <code>warn</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    warn: =&gt;
        console.warn.apply <span class="keyword">this</span>, arguments <span class="keyword">if</span> settings.General.debug
        msg = <span class="property">@getMessage</span> arguments

        <span class="keyword">if</span> loggly?
            msg.unshift settings.Log.Loggly.token
            logger.log.apply logger, msg
        <span class="keyword">else</span>
            logger.warning.apply logger, msg</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Log any object to the default transports as <code>error</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    error: =&gt;
        console.error.apply <span class="keyword">this</span>, arguments <span class="keyword">if</span> settings.General.debug
        msg = <span class="property">@getMessage</span> arguments

        <span class="keyword">if</span> loggly?
            msg.unshift settings.Log.Loggly.token
            logger.log.apply logger, msg
        <span class="keyword">else</span>
            logger.err.apply logger, msg</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>Log security related info to the default transports as <code>warn</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    security: =&gt;
        console.warn.apply <span class="keyword">this</span>, arguments <span class="keyword">if</span> settings.General.debug
        msg = <span class="property">@getMessage</span> arguments

        <span class="keyword">if</span> loggly?
            msg.unshift settings.Log.Loggly.token
            logger.log.apply logger, msg
        <span class="keyword">else</span>
            logger.warning.apply logger, msg</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <h2>HELPER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Serializes the parameters and return a JSON object representing the log message.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getMessage: -&gt;
        arr = []</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Parse all arguments and stringify objects.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">for</span> a <span class="keyword">in</span> arguments
            <span class="keyword">if</span> lodash.isArray a
                <span class="keyword">for</span> b <span class="keyword">in</span> a
                    arr.push b
            <span class="keyword">else</span> <span class="keyword">if</span> lodash.isObject a
                <span class="keyword">try</span>
                    arr.push JSON.stringify a
                <span class="keyword">catch</span> ex
                    arr.push a
            <span class="keyword">else</span>
                arr.push a</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Append IP address, if <code>currentIP</code> is set.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        arr.push <span class="string">"IP <span class="subst">#{currentIP}</span>"</span> <span class="keyword">if</span> currentIP?

        <span class="keyword">return</span> arr</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <h2>Singleton implementation</h2>

            </div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Logger.<span class="function"><span class="title">getInstance</span></span> = -&gt;
    <span class="property">@instance</span> = <span class="keyword">new</span> Logger() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.exports = exports = Logger.getInstance()</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
